package emailserving.pageView;

import com.google.common.collect.Lists;
import emailserving.domain.Job;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static emailserving.component.EmailTaskPool.JobExecutingStatus;

/**
 * Created by jack on 1/30/16.
 */
public class JobValueObject {

    private static final DecimalFormat decimalFormat = new DecimalFormat("00.00");

    private long id;
    private String startTime;
    private String finishTime;
    private String usedMinute;
    private String name;
    private long tasksTotalCount;
    private long aliveTasksCount;
    private long hadBeenSentEmailCount;
    private long contactsTotalCount;
    private String percentComplete;

    public static List<JobValueObject> createByJobs(final List<Job> jobs, final List<JobExecutingStatus> jobExecutingStatusList) {
        if (jobs != null) {
            return jobs.stream()
                    .filter(j -> j != null)
                    .map(
                        job -> convertJobToJobValueObject(job, jobExecutingStatusList))
                    .collect(Collectors.toList());
        } else {
            return Lists.newArrayList();
        }
    }

    private static JobValueObject convertJobToJobValueObject(final Job job, final List<JobExecutingStatus> jobExecutingStatusList) {
        JobValueObject jobValueObject = new JobValueObject();
        jobValueObject.startTime = formatDate(job.getStartTime());
        jobValueObject.finishTime = formatDate(job.getFinishTime());
        jobValueObject.usedMinute = String.valueOf(job.getUsedMinute());
        jobValueObject.name = job.getName();
        jobValueObject.id = job.getId();
        jobValueObject.hadBeenSentEmailCount = job.hadBeenSentEmailCount();
        jobValueObject.percentComplete = decimalFormat.format(job.percentComplete());
        jobValueObject.contactsTotalCount = job.contactsTotalCount();
        JobExecutingStatus jobExecutingStatus = pickJobRunningStatusFrom(job.getId(), jobExecutingStatusList);
        jobValueObject.tasksTotalCount = jobExecutingStatus.getTasksTotalCount();
        jobValueObject.aliveTasksCount = jobExecutingStatus.getAliveTasksCount();
        return jobValueObject;
    }

    public static JobExecutingStatus pickJobRunningStatusFrom(long jobId, List<JobExecutingStatus> jobStatus) {
        if (jobStatus != null && jobStatus.size() > 0) {
            for (JobExecutingStatus jobExecutingStatus : jobStatus) {
                if (Objects.equals(jobExecutingStatus.getJobId(), jobId)) {
                    return jobExecutingStatus;
                }
            }
            return new JobExecutingStatus(0, 0, 0);
        } else {
            return new JobExecutingStatus(0, 0, 0);
        }

    }


    private static String formatDate(Date date) {
        if (date != null) {
            DateTimeFormatter fmt = DateTimeFormat.forPattern("YYYY.MM.DD hh:mm");
            return fmt.print(new DateTime(date));
        } else {
            return "";
        }
    }

    public String getUsedMinute() {
        return usedMinute;
    }

    public long getId() {
        return id;
    }

    public String getStartTime() {
        return startTime;
    }

    public String getFinishTime() {
        return finishTime;
    }

    public String getName() {
        return name;
    }

    public long getTasksTotalCount() {
        return tasksTotalCount;
    }

    public long getHadBeenSentEmailCount() {
        return hadBeenSentEmailCount;
    }


    public long getContactsTotalCount() {
        return contactsTotalCount;
    }

    public String getPercentComplete() {
        return percentComplete;
    }

    public long getAliveTasksCount() {
        return aliveTasksCount;
    }
}
